/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.surveilans.controller;

import com.surveilans.classes.*;
import com.surveilans.model.DataModel;
import com.surveilans.model.UnitModel;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Yudi Retanto
 */
public class AdminController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setAttribute("title", "Unit Surveilans");
        UnitModel unit = new UnitModel();
        DataModel data = new DataModel();
        // UNIT SURVEILANS
        if (request.getParameter("action")==null) {
            if(request.getSession().getAttribute("authenticated")!=null){
                //logged in
                ArrayList<Unit> au = unit.getUnit();
                ArrayList<Provinsi> ap = unit.getProvinsi();
                
                request.setAttribute("unit", au);
                request.setAttribute("provinsi", ap);
                request.getRequestDispatcher("unit/list.jsp").forward(request, response);
            }else{
                response.sendRedirect("./home");
            }
        }else if(request.getParameter("action").equals("add_unit")){
            String nama = request.getParameter("nama");
            String alamat = request.getParameter("alamat");
            int provinsi = Integer.valueOf(request.getParameter("prov"));
            int kabupaten = Integer.valueOf(request.getParameter("kab"));
            String username = request.getParameter("user");
            String password = request.getParameter("pwd");
            unit.addUnit(nama,alamat,provinsi, kabupaten, username, password);
            request.getSession().setAttribute("flash_type", "success");
            request.getSession().setAttribute("flash", "Unit telah ditambahkan ke dalam sistem");
            response.sendRedirect("./admin");
        }else if(request.getParameter("action").equals("edit_unit")){
            if (request.getMethod().equals("POST")) {
                int id = Integer.valueOf(request.getParameter("id"));
                Unit u = unit.getUnit(id);
                u.setNama(request.getParameter("nama"));
                u.setAlamat(request.getParameter("alamat"));
                u.setProvinsi(Integer.valueOf(request.getParameter("prov")));
                u.setKabupaten(Integer.valueOf(request.getParameter("kab")));
                u.setUsername(request.getParameter("user"));
                unit.updateUnit(u);
                String password = request.getParameter("pwd");
                if(!password.equalsIgnoreCase("")){
                    unit.updateUnitPwd(id, password);
                }
                request.getSession().setAttribute("flash_type", "success");
                request.getSession().setAttribute("flash", "Unit telah berhasil diupdate");
                response.sendRedirect("./admin");
            }else{
                int id = Integer.valueOf(request.getParameter("id"));
                Unit u = unit.getUnit(id);
                ArrayList<Provinsi> ap = unit.getProvinsi();
                ArrayList<Kabupaten> ak = unit.getKabupaten(u.getProvinsi());
                
                request.setAttribute("unit", u);
                request.setAttribute("provinsi", ap);
                request.setAttribute("kabupaten", ak);
                request.getRequestDispatcher("unit/edit.jsp").forward(request, response);
            }
        }else if(request.getParameter("action").equals("delete_unit")){
            int id = Integer.valueOf(request.getParameter("id"));
            unit.deleteUnit(id);
            request.getSession().setAttribute("flash_type", "success");
            request.getSession().setAttribute("flash", "Unit telah berhasil dihapus dari sistem");
            response.sendRedirect("./admin");
        }
        // DATA SURVEILANS
        else if(request.getParameter("action").equals("list_data")){
            request.setAttribute("title", "Data Surveilans");
            ArrayList<DataSurveilans> ad = data.getDataPenyakit();
            request.setAttribute("data", ad);
            request.setAttribute("unitModel", unit);
            request.getRequestDispatcher("data/list.jsp").forward(request, response);
        }
        else if(request.getParameter("action").equals("edit_data")){
            request.setAttribute("title", "Data Surveilans");
            if (request.getMethod().equals("POST")) {
                DataSurveilans ds = new DataSurveilans();
                ds.setId(Integer.valueOf(request.getParameter("id")));
                ds.setRange1(Integer.valueOf(request.getParameter("pen[1]")));
                ds.setRange2(Integer.valueOf(request.getParameter("pen[2]")));
                ds.setRange3(Integer.valueOf(request.getParameter("pen[3]")));
                ds.setRange4(Integer.valueOf(request.getParameter("pen[4]")));
                ds.setRange5(Integer.valueOf(request.getParameter("pen[5]")));
                ds.setRange6(Integer.valueOf(request.getParameter("pen[6]")));
                ds.setRange7(Integer.valueOf(request.getParameter("pen[7]")));
                ds.setRange8(Integer.valueOf(request.getParameter("pen[8]")));
                ds.setRange9(Integer.valueOf(request.getParameter("pen[9]")));
                ds.setRange10(Integer.valueOf(request.getParameter("pen[10]")));
                ds.setRange11(Integer.valueOf(request.getParameter("pen[11]")));
                ds.setRange12(Integer.valueOf(request.getParameter("pen[12]")));
                data.updateDataSurveilans(ds);
                request.getSession().setAttribute("flash_type", "success");
                request.getSession().setAttribute("flash", "Data surveilans telah berhasil diedit");
                response.sendRedirect("./admin?action=list_data");
            }else{
                int id_data = Integer.valueOf(request.getParameter("id"));
                DataSurveilans ds = data.getDataSurveilans(id_data);
                request.setAttribute("data", ds);
                request.getRequestDispatcher("data/edit.jsp").forward(request, response);
            }
        }else if(request.getParameter("action").equals("delete_data")){
            int id = Integer.valueOf(request.getParameter("id"));
            data.deleteDataSurveilans(id);
            request.getSession().setAttribute("flash_type", "success");
            request.getSession().setAttribute("flash", "Data surveilans telah berhasil dihapus dari sistem");
            response.sendRedirect("./admin?action=list_data");
        }else if(request.getParameter("action").equals("ajax")){
            if(request.getParameter("data")==null){
                //null
            }
            else if(request.getParameter("data").equals("kabupaten")){
                int id_prov = Integer.valueOf(request.getParameter("id_prov"));
                ArrayList<Kabupaten> ak = unit.getKabupaten(id_prov);
                response.setContentType("text/plain");
                PrintWriter out = response.getWriter();
                Iterator itr = ak.iterator();
                while (itr.hasNext()) {
                    Kabupaten k = (Kabupaten) itr.next();
                    out.println("<option value=\"" + k.getId() + "\">" + k.getNama() + "</option>");
                }                
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
